package com.mangohouse.common;

import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
 * 
 * @TODO 从配置文件中读取信息
 * @author roye
 * @date May 22, 2013 11:05:59 AM
 * @version V1.0
 */
public class ReadConfigProperties {
	
	private static Logger logger = Logger.getLogger(ReadConfigProperties.class); 

	/**
	 * 基础路径
	 */
	public static String FILEUPLOAD_BASE_PATH = "";
	
	public static String URL_SEPARATOR = "/";
	
	/**
	 * 配置文件路径必需放在 项目的webapp文件夹下
	 */
	private static Properties prop = null;

	/**
	 * 初始化属性
	 */
	static {
		String path = "/config/windows.properties";
		if (isLinux()) {
			path = "/config/linux.properties";
		}
		InputStream in = ReadConfigProperties.class.getResourceAsStream(path);
		if (in != null) {
			prop = new Properties();
			try {
				prop.load(in);
				FILEUPLOAD_BASE_PATH = (String) prop.get("BASE_PATH");
			} catch (Exception e) {
				logger.error("-----------加载config配置文件错误!----------");
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * 
	 * @todo  读取配置文件，根据KEY值获取值域
	 * @author roye
	 * @date May 22, 2013 11:13:32 AM
	 * @param   
	 * @return String   
	 * @throws
	 */
	public static String getPropValue(String key) throws Exception {
		String path = "/config/windows.properties";
		String val = null;
		if (isLinux()) {
			path = "/config/linux.properties";
		}
		InputStream in = ReadConfigProperties.class.getResourceAsStream(path);
		if (in != null) {
			prop = new Properties();
			try {
				prop.load(in);
				val = prop.getProperty(key);
			} catch (Exception e) {
				logger.error("-----------获取config配置文件中的属性值错误!----------");
				throw e;
			}
		}
		return val;
	}

	/**
	 * 
	 * @todo  系统判断
	 * @author roye
	 * @date May 22, 2013 11:14:14 AM
	 * @param   
	 * @return boolean   
	 * @throws
	 */
	public static boolean isLinux() {
		String osType = System.getProperties().getProperty("os.name").toLowerCase();
		if (osType.startsWith("windows")) {
			return false;
		} else {
			return true;
		}
	}
	
}
